/*
 * Hyuga: A Density-Grid Stream Clustering Platform.
 * Copyright (C) 2014 PUC-Rio/Laboratory for Advanced Collaboration
 *
 * Hyuga is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Hyuga is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hyuga.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.pucrio.inf.lac.konoha.hyuga.events.grid;

import br.pucrio.inf.lac.konoha.hyuga.events.CEPEvent;
import br.pucrio.inf.lac.konoha.hyuga.events.CEPEvent.TYPE;
import br.pucrio.inf.lac.konoha.hyuga.events.cell.CellClusterEvent2;
import br.pucrio.inf.lac.konoha.hyuga.events.cell.CellContentEvent;

/**
 * Represent a grid cluster event. This event is formed by a set of context partition (cells) clusters that represent
 * the cluster, i.e., it is a cluster of contexts partition clusters. It contains the core partitions that form and
 * shapes the cluster. Note that this is not the final cluster, in addition to the core context partitions, the final
 * cluster contains the border partitions of these core partitions.
 * 
 * @author Marcos Roriz <mroriz@inf.puc-rio.br>
 */
public class GridClusterEvent extends CEPEvent {
    private CellClusterEvent2[] cells;
    private CellContentEvent[] neighbors;

    // No arg constructor
    public GridClusterEvent() {}
    
    /**
     * Grid (set of Cells/Partitions) that forms the cluster.
     * @param cells The core cells.
     */
    public GridClusterEvent(CellClusterEvent2[] cells) {
        this(cells, null);
    }
    
    /**
     * Grid (set of Cells/Partitions) that forms the cluster.
     * @param cells The core cells.
     * @param neighbors The neighbor (border) cells.
     */
    public GridClusterEvent(CellClusterEvent2[] cells, CellContentEvent[] neighbors) {
        this.cells     = cells;
        this.neighbors = neighbors;
        this.type      = TYPE.GRIDCLUSTER;
    }

    public boolean hasSameCells(GridClusterEvent otherCluster) {
        return this.cellHashSum() == otherCluster.cellHashSum();
    }

    private double cellHashSum() {
        int hash  = 0;
        int hashX = 0;
        int hashY = 0;
        for (CellClusterEvent2 cell : cells) {
            hashX += cell.getX() * cell.getX();
            hashY += cell.getY() * cell.getY();
            hash  += cell.getX() * cell.getY();
        }
        
        return hash + hashX + hashY;
    }
    
    //////////////////////////////////////////////////////////////////////////
    // Gets and Sets
    //////////////////////////////////////////////////////////////////////////

    public CellClusterEvent2[] getCells() {
        return cells;
    }

    public void setCells(CellClusterEvent2[] cells) {
        this.cells = cells;
    }

    public CellContentEvent[] getNeighbors() {
        return neighbors;
    }

    public void setNeighbors(CellContentEvent[] neighbors) {
        this.neighbors = neighbors;
    }
}
